﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

using eBook.Core;
using eBook.Core.BusinessObjects;
using eBook.Core.Contracts;
using eBook.EFRepositories;

namespace eBookWinApp
{
  public class BookRenamer
  {

    private boBookInfo _searchResult;
    private ILibRepository _repository;

    public string LibraryBaseFolder
    {
      get;
      set;
    }
    public string SourceFilePath
    {
      get;
      set;
    }
    public string DestinationFilePath
    {
      get;
      set;
    }


    public BookRenamer(ILibRepository repository)
    {
      _repository = repository;
    }
   

    private string SuggestNewFilePath( ISBN isbn, boBookInfo bookInfo )
    {      
      if ( string.IsNullOrEmpty(this.SourceFilePath) )
        return string.Empty;
      
      if ( !File.Exists(this.SourceFilePath) )
        return string.Empty;
        
      #region Build folder name  
      
      string newFolderName = string.Empty;
      string[] iParts = isbn.Hyphenate().Split( '-' );
      if ( iParts.Length >= 4 )
        newFolderName = string.Format("{0}\\{1}\\{2}"
                                    , this.LibraryBaseFolder 
                                    , iParts[0] 
                                    , iParts[1]);
      else
        newFolderName = string.Format("{0}\\{1}"
                                    , this.LibraryBaseFolder 
                                    , iParts[0]);

      #endregion  
      
      #region Build file name 
      FileInfo f = new FileInfo( this.SourceFilePath );
      string newFilePath = string.Format( "{0}\\[{1}] {2}{3}"
                                        , newFolderName
                                        , isbn.Hyphenate()
                                        , ReplaceInvalidFilenameChar( bookInfo.Title )
                                        , f.Extension ); 
      #endregion
                                                                                
      return newFilePath;
    }      
  
    //public void Rename()
    //{
    //  if ( _searchResult == null)
    //    return;

    //  #region Persist book entity 
      
    //  Book book = ConvertInfoToEntity( _searchResult );
    //  if ( book != null )
    //    _provider.SaveOrUpdate( book ); 
           
    //  #endregion              
            
          
    //  #region Move file 
      
    //  if ( File.Exists(this.DestinationFilePath))
    //    return;    
        
    //  string destinationFolder = Path.GetDirectoryName( this.DestinationFilePath );      
    //  if ( !Directory.Exists( destinationFolder ) )
    //    Directory.CreateDirectory( destinationFolder );  
        
    //  FileInfo f = new FileInfo( this.SourceFilePath );
    //  f.MoveTo( this.DestinationFilePath );    
      
    //  book.FilePath = destinationFolder;
    //  book.FileName = f.Name;     
    //  _provider.SaveOrUpdate(book);
       
    //  #endregion 
    

         
    //}
    
    //public Book ConvertInfoToEntity(boBookInfo awsBook)
    //{      
    //  Book book = TryToGetBookByISBN(awsBook);
    //  if (book == null)      
    //    book = new Book();
        
    //  ISBN isbn10 = new ISBN10(awsBook.ISBN10);
    //  book.ISBN10 = isbn10.Hyphenate();
    //  book.ISBN13 = new ISBN13(isbn10.Transform()).Hyphenate();
      
    //  book.ASIN   = awsBook.ASIN;
    //  book.DeweyDecimal  = awsBook.DeweyDecimalNumber;
      
    //  book.Title   = awsBook.Title;
    //  book.Edition = awsBook.Edition;
      
    //  if ( !string.IsNullOrEmpty(awsBook.NumberOfPages) )
    //    book.NumberOfPages = Convert.ToInt32(awsBook.NumberOfPages);
        
    //  book.EditorialReview = awsBook.EditorialReview;
      
    //  book.AmazonRating = awsBook.AudienceRating;
    //  book.PublicationDate = awsBook.PublicationDate;
      
    //  book.SmallImageURL = awsBook.SmallImageURL;
    //  book.MediumImageURL = awsBook.MediumImageURL;
    //  book.LargeImageURL = awsBook.LargeImageURL;
    //  book.DateCreated = DateTime.Now;
      
    //  foreach ( string author in awsBook.Authors )
    //  {
    //    bool hasAuthor = 1 == book.Authors.Count<Author>( x => x.Name == author );      ;
    //    if ( !hasAuthor )
    //      book.Authors.Add(new Author(){Name = author});
    //  }
            
    //  bool hasLink = 1 == book.Links.Count<Link>( x => x.Path == awsBook.DetailPageURL );      
    //  if ( !hasLink )
    //    book.Links.Add(new Link(){Name = "Amazon detail page", Path = awsBook.DetailPageURL});
      
    //  Publisher p =_provider.GetPublisherByName(awsBook.Publisher);
    //  if ( p == null )
    //    p = new Publisher(){Name=awsBook.Publisher};
        
    //  book.Publisher = p;  
      
    //  return book;
    //}  
    
    //private Book TryToGetBookByISBN(boBookInfo awsBook)
    //{
    //  Book result = null;
      
    //  ISBN13 isbn13 = null;
    //  ISBN10 isbn10 = new ISBN10(awsBook.ISBN10);    
      
    //  result = _provider.GetBookByISBN( isbn10.Hyphenate() );      
    //  if (result != null)
    //    return result;
        
    //  isbn13 = new ISBN13(isbn10.Transform());
    //  result = _provider.GetBookByISBN( isbn13.Hyphenate() );      
    //  if (result != null)
    //    return result;
        
    //  isbn13 = new ISBN13(awsBook.ISBN13);
    //  result =_provider.GetBookByISBN( isbn13.Hyphenate() );
    //  if ( result != null)
    //    return result;

    //  isbn10 = new ISBN10(isbn13.Transform());
    //  result = _provider.GetBookByISBN( isbn10.Hyphenate() );
      
    //  return result;
    //}
    

    
    private string ReplaceInvalidFilenameChar(string filename)
    {
      string result = new Regex(@"[:\\/?<>|\*]").Replace(filename, " ");
      return result;
    }
    
  
  }
}
